library(readr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(stringr)
install.packages("plotly")
##
## The downloaded binary packages are in
## /var/folders/fz/vk3jh7714xvg6zgxgtntpp1c0000gn/T//RtmpEv2fNn/downloaded_packages
install.packages("countrycode")
##
## The downloaded binary packages are in
## /var/folders/fz/vk3jh7714xvg6zgxgtntpp1c0000gn/T//RtmpEv2fNn/downloaded_packages
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(countrycode)
# Load data
mortality <- read_csv("Maternal mortality ratio.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
## dat <- vroom(...)
## problems(dat)
## Rows: 185 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): name, slug, ranking
## dbl (2): 000 live births, date_of_information
## num (1): deaths/100
## lgl (1): region
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
vulnerability <- read_csv("vulnerability.csv")
## Rows: 192 Columns: 30
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): ISO3, Name
## dbl (28): 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, ...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Clean mortality data
names(mortality) <- str_trim(names(mortality))
mortality <- mortality %>%
rename(Country = name, Mortality = `deaths/100`) %>%
mutate(Mortality = as.numeric(gsub(",", "", Mortality)))
# Prepare vulnerability data (2022 column)
vulnerability <- vulnerability %>%
rename(Country = Name) %>%
select(Country, Vulnerability = `2022`)
# Merge datasets
data <- inner_join(mortality, vulnerability, by = "Country")
# Add ISO3 codes for Plotly
data$ISO3 <- countrycode(data$Country, "country.name", "iso3c")
# Create choropleth map
fig <- plot_ly(data = data,
type = 'choropleth',
locations = ~ISO3,
z = ~Mortality,
text = ~paste(Country,
"<br>Mortality:", round(Mortality, 1),
"<br>Vulnerability:", round(Vulnerability, 3)),
colorscale = 'Reds',
marker = list(line = list(color = 'gray', width = 0.5)),
colorbar = list(
title = "Maternal Mortality",
len = 0.5,
thickness = 10,
x = 1.05
))
fig <- plot_ly(data = data,
type = 'choropleth',
locations = ~ISO3,
z = ~Mortality,
text = ~paste(Country,
"<br>Mortality:", round(Mortality, 1),
"<br>Vulnerability:", round(Vulnerability, 3)),
colorscale = 'Reds',
marker = list(line = list(color = 'gray', width = 0.5)),
colorbar = list(
title = "Maternal Mortality",
len = 0.5, # Reduce vertical size
thickness = 10, # Make it slimmer
x = 1.05 # Slightly offset to the right
))
fig <- fig %>%
layout(
title = 'Maternal Mortality Ratio & Climate Vulnerability (2022)',
geo = list(
showframe = FALSE,
showcoastlines = FALSE,
projection = list(type = 'natural earth'),
bgcolor = 'white'
)
)
fig
library(htmlwidgets)
saveWidget(fig, "maternal_mortality_map.html", selfcontained = TRUE)